﻿using Mybook.Models;
using MyStory.Service;
using MyStory.util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace Mybook.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class BookDetail : ContentPage
    {
        private FictionService _fictionService;
        private List<FictionModel> repositorieschapter;
        FictionModel model; 
        private string fictionid;
        int totel;
        public BookDetail(FictionModel fictionModel)
        {
            fictionid = fictionModel.FictionId;
            InitializeComponent();
            BookDetailasync(fictionid);
            title.Text = fictionModel.Title;
            image.Source = fictionModel.ImageUrl;
           
        }
        public async void BookDetailasync(string fictionidname)
        {
            
            _fictionService = new FictionService();
            repositorieschapter = await _fictionService.GetChapterRepositoriesAsync(ConnectUrl.GetChapterUrl(fictionidname));
           
            collectionView.ItemsSource = repositorieschapter;
            totelnum.Text = "总章节：" + repositorieschapter.Count().ToString();



        }
        private async void collectionView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
        {
            model = new FictionModel();
            model=(FictionModel)e.SelectedItem;
            await Navigation.PushAsync(new BookContent(model, repositorieschapter));
        }
        ViewCell lastCell;
        private void ViewCell_Tapped(object sender, EventArgs e)
        {
            if (lastCell != null)
                lastCell.View.BackgroundColor = Color.AntiqueWhite;
            var viewCell = (ViewCell)sender;
            if (viewCell.View != null)
            {
                viewCell.View.BackgroundColor = Color.AliceBlue;
                lastCell = viewCell;
            }
        }
        private async void backPage(object sender, EventArgs e)
        {
            await Navigation.PopAsync();
        }
    }
}